package com.jye.rseven.user.serivce.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.jye.rseven.domain.PtInfo;
import com.jye.rseven.domain.TyPtCode;
import com.jye.rseven.domain.TzPtCode;
import com.jye.rseven.domain.User;
import com.jye.rseven.service.PtInfoService;
import com.jye.rseven.user.mapper.PtInfoMapper;
import com.jye.rseven.user.mapper.TyPtCodeMapper;
import com.jye.rseven.user.mapper.TzPtCodeMapper;
import com.jye.rseven.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class PtInfoServiceImpl implements PtInfoService {

    @Autowired
    PtInfoMapper ptInfoMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    TzPtCodeMapper tzPtCodeMapper;

    @Autowired
    TyPtCodeMapper tyPtCodeMapper;

    @Override
    public int addPtInfo(PtInfo ptInfo) {
        try{
            ptInfo.setAddTime(new Date());
            ptInfo.setEndTime(new Date());
            ptInfoMapper.addPtInfo(ptInfo);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public String findPtCodeById(int id) {
        return ptInfoMapper.findPtCodeById(id);
    }

    @Override
    public void addOnePtNumByNumberAndPtCode(int number, String ptCode) {
        ptInfoMapper.addOnePtNumByNumberAndPtCode(number,ptCode);
    }

    @Override
    public PtInfo findPtInfoById(Integer id) {
        return ptInfoMapper.findPtInfoById(id);
    }

    @Override
    public List<PtInfo> findPtInfoByUId(Integer uId) {
        return ptInfoMapper.findPtInfoByUId(uId);
    }

    @Override
    public int findPtNumByPtCode(String ptCode) {
        return ptInfoMapper.findPtNumByPtCode(ptCode);
    }

    @Override
    public List<PtInfo> findPtByUId(int uid, int skip, int limit) {
        User u = userMapper.findUserById(uid);
        List<PtInfo> list = ptInfoMapper.findPtByUId(uid,skip,limit);
        for (PtInfo pt: list){
            pt.setUser(u);
        }

        return list;
    }

    @Override
    public PtInfo findPtById(int id) {
        PtInfo pt = ptInfoMapper.findPtById(id);
        User u = userMapper.findUserById(pt.getUId());

        pt.setUser(u);
        return pt;
    }

    @Override
    public List<PtInfo> findAllPt(int skip, int limit) {
        List<PtInfo> ptlist = ptInfoMapper.findAllPt(skip, limit);
        for (PtInfo pt: ptlist )
        {
            User user ;
            TzPtCode tz = tzPtCodeMapper.findTzByPtCode(pt.getPtCode());
            List<TyPtCode> ty = tyPtCodeMapper.findTyByPtCode(pt.getPtCode());
            for (TyPtCode ty1:ty){
                user = userMapper.findUserById(ty1.getUId());
                if (user != null)
                    ty1.setUser(user);
            }
            tz.setUser(userMapper.findUserById(tz.getUId()));
            pt.setTzPtCode(tz);
            pt.setTyPtCode(ty);
            pt.setUser(userMapper.findUserById(pt.getUId()));
        }
        return ptlist;
    }

    @Override
    public PtInfo findTzPtInfoByPtCode(String ptCode) {
        return ptInfoMapper.findTzPtInfoByPtCode(ptCode);
    }

    @Override
    public List<PtInfo> findAllByGoodsId(int goodsId, int skip, int limit) {
        List<PtInfo> ptlist = ptInfoMapper.findAllPtByGoodsId(goodsId,skip, limit);
        for (PtInfo pt: ptlist )
        {
            User user ;
            TzPtCode tz = tzPtCodeMapper.findTzByPtCode(pt.getPtCode());
            List<TyPtCode> ty = tyPtCodeMapper.findTyByPtCode(pt.getPtCode());
            for (TyPtCode ty1:ty){
                user = userMapper.findUserById(ty1.getUId());
                if (user != null)
                    ty1.setUser(user);
            }
            tz.setUser(userMapper.findUserById(tz.getUId()));
            pt.setTzPtCode(tz);
            pt.setTyPtCode(ty);
            pt.setUser(userMapper.findUserById(pt.getUId()));
        }
        return ptlist;
    }

    @Override
    public List<PtInfo> findAllByPtCode(String ptCode, int skip, int limit) {
        List<PtInfo> ptlist = ptInfoMapper.findAllByPtCode(ptCode,skip, limit);
        for (PtInfo pt: ptlist )
        {
            User user ;
            TzPtCode tz = tzPtCodeMapper.findTzByPtCode(pt.getPtCode());
            List<TyPtCode> ty = tyPtCodeMapper.findTyByPtCode(pt.getPtCode());
            for (TyPtCode ty1:ty){
                user = userMapper.findUserById(ty1.getUId());
                if (user != null)
                    ty1.setUser(user);
            }
            tz.setUser(userMapper.findUserById(tz.getUId()));
            pt.setTzPtCode(tz);
            pt.setTyPtCode(ty);
            pt.setUser(userMapper.findUserById(pt.getUId()));
        }
        return ptlist;
    }


}
